package com.emm.yixun.website.controller.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.common.model.Resources;
import com.emm.yixun.common.model.TreeNode;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.ResourcesDto;
import com.emm.yixun.common.model.dto.UserResourcesDto;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.system.service.ResourcesServiceFacade;
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.base.Const;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;
@Controller
@RequestMapping("/system/permissionAssignment/")
public class PermissionAssignmentController  extends BaseController<Object>{
	
	@Autowired
	private ResourcesServiceFacade resourcesServiceFacade;
	
	@RequestMapping(value="index")
	public String index(ModelMap map){
		User user = ShiroUtil.getCurrentUser();
		//查询所有的模块
		UserResourcesDto userResourcesDto = new UserResourcesDto();
		//菜单类型
		userResourcesDto.setResourcesLevel("0");
		userResourcesDto.setMerchantId(user.getMerchantId());
		userResourcesDto.setParentCode("menu_0000");
		ResponseList<Resources> responseList = merchantResourcesServiceFacade.findUserResources(userResourcesDto);
		if(responseList.isSuccess() && null!=responseList.getEntity()){
			JSONArray modelArray=new JSONArray();
			for(Resources resources : responseList.getEntity()){
				JSONObject obj=new JSONObject();
				obj.put("modelName", resources.getResourcesName());
				obj.put("modelCode", resources.getResourcesCode());
				modelArray.add(obj);
			}
			map.put("modelList", modelArray.toJSONString());
		}
		return "/components/permissionAssignment";
	}
	
	//查询菜单/按钮数据
	
	//查询系统资源
	@RequestMapping(value="listAuthorized", method=RequestMethod.POST)
	@ResponseBody
	public AjaxRes listAuthorized(String roleId,String parentCode){
    	AjaxRes ar=getAjaxRes();
    	try {
    		
    		User user=ShiroUtil.getCurrentUser();
			UserResourcesDto userResourcesDto=new UserResourcesDto();
			userResourcesDto.setMerchantId(user.getMerchantId());
			if(StringUtils.isNotBlank(parentCode))
			  userResourcesDto.setParentCode(parentCode);
			ResponseList<Resources> responseList=merchantResourcesServiceFacade.findUserResources(userResourcesDto);
			
/*			RoleResourcesDto roleResourcesDto=new RoleResourcesDto();
    		roleResourcesDto.setRoleId(Long.valueOf(roleId));
    		roleResourcesDto.setMerchantId(user.getMerchantId());;
    		ResponseList<RoleResources> authorizedList=roleResourcesServiceFacade.findByDto(roleResourcesDto);*/
			ResourcesDto resourcesDto=new ResourcesDto();
			resourcesDto.setResourcesCode(parentCode);
			Resources resources=resourcesServiceFacade.findByDto(resourcesDto).getEntity().get(0);
			
			//组装Znodes
			List<TreeNode> list=new ArrayList<TreeNode>();
			TreeNode topTreeNode = new TreeNode();
			topTreeNode.setId(resources.getResourcesCode());
			topTreeNode.setText(resources.getResourcesName());
			//topTreeNode.setChecked(false);
			topTreeNode.setLeaf(false);
			
			List<TreeNode> children=new ArrayList<TreeNode>();
			if(responseList.isSuccess() && null!=responseList.getEntity()){
				for(Resources r : responseList.getEntity() ){
					TreeNode treeNode = new TreeNode();
					treeNode.setId(r.getResourcesCode());
					treeNode.setText(r.getResourcesName());
					//treeNode.setChecked(false);
					treeNode.setLeaf(true);
					children.add(treeNode);
    			}
			}
			topTreeNode.setChildren(children);
			list.add(topTreeNode);
			ar.setSucceed(list);
		} catch (Exception e) {
			logger.error(e.toString(),e);
			ar.setFailMsg(Const.DATA_FAIL);
		}
    	return ar;
	}
}
